﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace YunHao
{
    /// <summary>
    /// 加密解密字符串
    /// </summary>
    public class CryptoString
    {
        private static string sKEY = @"50902FE00DA45BE8B416C03098618C8F";
        private static string sIV = @"4rZymEMfa/PpeJ89qY4gyA==";

        /// <summary>
        /// 设置秘钥信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        public static void SetCryptoInfo(string key, string iv)
        {
            sKEY = key;
            sIV = iv;
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="encString"></param>
        /// <returns></returns>
        public static string Decrypt(string encString)
        {
            RijndaelManaged rijndaelManaged = new RijndaelManaged
            {
                Padding = PaddingMode.Zeros,
                Mode = CipherMode.CBC,
                KeySize = 128,
                BlockSize = 128
            };
            byte[] bytes = Encoding.UTF8.GetBytes(sKEY);
            byte[] rgbIV = Convert.FromBase64String(sIV);
            ICryptoTransform transform = rijndaelManaged.CreateDecryptor(bytes, rgbIV);
            byte[] array = Convert.FromBase64String(encString);
            byte[] array2 = new byte[array.Length];
            MemoryStream stream = new MemoryStream(array);
            CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read);
            cryptoStream.Read(array2, 0, array2.Length);
            return Encoding.UTF8.GetString(array2).TrimEnd(new char[1]);
        }

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="rawString"></param>
        /// <returns></returns>
        public static string Encrypt(string rawString)
        {
            RijndaelManaged rijndaelManaged = new RijndaelManaged
            {
                Padding = PaddingMode.Zeros,
                Mode = CipherMode.CBC,
                KeySize = 128,
                BlockSize = 128
            };
            byte[] bytes = Encoding.UTF8.GetBytes(sKEY);
            byte[] rgbIV = Convert.FromBase64String(sIV);
            ICryptoTransform transform = rijndaelManaged.CreateEncryptor(bytes, rgbIV);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
            byte[] bytes2 = Encoding.UTF8.GetBytes(rawString);
            cryptoStream.Write(bytes2, 0, bytes2.Length);
            cryptoStream.FlushFinalBlock();
            byte[] inArray = memoryStream.ToArray();
            return Convert.ToBase64String(inArray);
        }

        /// <summary>
        /// 获取字符串的hash值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetHash(string key)
        {
            MD5 mD = new MD5CryptoServiceProvider();
            byte[] array = mD.ComputeHash(Encoding.UTF8.GetBytes(key));
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.Append(array[i].ToString("x2"));
            }
            return stringBuilder.ToString();
        }

    }
}